/* -------------------------------------------------------------------------------- /
	
	Magentech jQuery
	Created by Magentech
	v1.0 - 20.9.2016
	All rights reserved.
	
/ -------------------------------------------------------------------------------- */

	//必须先判断用户是否处于登录状态
	// Cart add remove functions
	var cart = {
		'add': function(goods_id,goods_name, quantity, goods_img,goods_now) {

			if (quantity==0) {
				quantity = $("input[name='quantity']").val();
			}
			$.post("/API/HomeUser/isLogin",null,function(ret){
				if (ret.status==true) {
					$.post("/API/Shopcart/addgoodstocart",{"goodsid":goods_id,"goodsname":goods_name,"goodsimg":goods_img,"goodsnow":goods_now,"quantity":quantity},function(res){
						if(res.status==true){

							addProductNotice('商品加入到购物车', '<img src="  '+goods_img+'  " alt="">', '<h3><a href="#">'+goods_name+' </a> 加入到 <a href="#">购物车</a>!</h3>', 'success');
							if (res.count==0) {
								var html = template('addgoods-tpl', res);
								//获取原有商品的总数量
								var totalnum = $("#goodstotalnumber").text();
								totalnum =parseInt(totalnum) +quantity;
								//获取原有商品的总价
								var totalprices =  $("#goodstotalprices").text();
								totalprices =parseInt(totalprices) +parseInt(goods_now);
								//改变添加商品后的商品价格与数量
								$("#goodstotalnumber").text(totalnum);
								$("#goodstotalprices").text(totalprices);
								$("#goodstotalprices1").text(totalprices);

								//在购物车中显示
								$("#shopcart-box").append(html);
							}else{
								var length = $("#shopcart-box tr").length;
								for (var i = 0; i < length; i++) {
									var goodsid=$("#shopcart-box").find("tr").eq(i).attr("cid");
									if (goodsid==goods_id) {
										//修改数量
										var number= $("#shopcart-box").find("tr").eq(i).find("td").eq(2).text();
										number =  number.substring(1);
										number = parseInt(number)+quantity;
										$("#shopcart-box").find("tr").eq(i).find("td").eq(2).text("x"+number);
										//获取原有商品的总数量
										var totalnum = $("#goodstotalnumber").text();
										totalnum =parseInt(totalnum) +quantity;
										//获取原有商品的总价
										var totalprices =  $("#goodstotalprices").text();
										totalprices =parseInt(totalprices) +parseInt(goods_now);
										$("#goodstotalnumber").text(totalnum);
										$("#goodstotalprices").text(totalprices);
										$("#goodstotalprices1").text(totalprices);
										break;
									}
								}
								
							}
						}

					});

					
				}else{
					addProductNotice('用户未登录', '<img src="  '+goods_img+'  " alt="">', '<h3>你必须先<a href="/Home/User/index">登录</a>商城才能把<a href="/Home/Goods/showgoods?goodsid='+goods_id+'  ">'+goods_name+'</a>加入到你的 <a href="#">购物车</a>!</h3>', 'success');
				}
			});
			
		}

		
	}

	var wishlist = {
		'add': function(goods_id,goods_name, goods_img) {
			$.post("/API/HomeUser/isLogin",null,function(ret){
				if (ret.status==true) {
					$.post("/API/Shopcart/addgoodstocol",{"goodsid":goods_id},function(res){
						if (res.status==true && res.count==1) {
							addProductNotice('商品添加到收藏列表', '<img src=" '+goods_img+' " alt="">', '<h3>成功:你已经添加 <a href="/Home/Goods/showgoods?goodsid='+goods_id+'  ">'+goods_name+'</a> 到你的 <a href="#">收藏列表</a>!</h3>', 'success');
						}else{
							addProductNotice('商品添加到收藏列表', '<img src=" '+goods_img+' " alt="">', '<h3>提醒:<a href="/Home/Goods/showgoods?goodsid='+goods_id+'  ">'+goods_name+'</a> 已经在你的 <a href="#">收藏列表</a>!</h3>', 'success');
						}
					})
					
				}else{
					addProductNotice('用户未登录', '<img src=" '+goods_img+' " alt="">', '<h3>你必须先<a href="/Home/User/index">登录</a>商城才能保存<a href="/Home/Goods/showgoods?goodsid='+goods_id+'  ">'+goods_name+'</a> 到你的 <a href="#">收藏列表</a>!</h3>', 'success');
				}
			});
			
		}
	}
	var compare = {
		'add': function(product_id,product_name, product_img) {
			$.post("/API/HomeUser/isLogin",null,function(ret){
				if (ret.status==true) {
					addProductNotice('商品加入到比较', '<img src=" '+product_img+' " alt="">', '<h3>成功: 你已经添加 <a href="#">'+product_name+'</a> 到你的 <a href="#">产品比较</a>!</h3>', 'success');
				}else{
					addProductNotice('用户未登录', '<img src=" '+product_img+' " alt="">', '<h3>你必须先<a href="#">登录</a>商城才能保存 <a href="#">'+product_name+'</a> 到你的 <a href="#">产品比较</a>!</h3>', 'success');
				}
			});
				
		}
	}

	/* ---------------------------------------------------
		jGrowl – jQuery alerts and message box
	-------------------------------------------------- */
	function addProductNotice(title, thumb, text, type) {
		$.jGrowl.defaults.closer = false;
		//Stop jGrowl
		//$.jGrowl.defaults.sticky = true;
		var tpl = thumb + '<h3>'+text+'</h3>';
		$.jGrowl(tpl, {		
			life: 4000,
			header: title,
			speed: 'slow',
			theme: type
		});
	}

